<?php
/**
 * Created by PhpStorm.
 * User: xionggang
 * Date: 15-4-28
 * Time: 下午3:37
 */
class Article_list extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model("admin/article_model");
        $this->load->model("admin/type_model");
        $this->load->model("admin/admin_model");
    }

    /**
     * auto:xg
     * 文章列表
     */
    public function index()
    {
        $typeid  = $this->input->get('typeid') ? $this->input->get('typeid',true) : 0;
        $writer = $this->input->get('writer') ? $this->input->get('writer',true) : '';
        $title  = $this->input->get('title') != '请输入文章标题' ? $this->input->get('title',true) : '';
        $page   = $this->input->get('page') ? intval($this->input->get('page',true)) : 1;
        $typeid =  $this->input->get('typeid') ? intval($this->input->get('typeid',true)) : 0;
        $is_del = $this->input->get('is_del') ? $this->input->get('is_del',true) : 0;
        $time_start = $this->input->get('time_start') ? $this->input->get('time_start',true) : '';
        $time_end = $this->input->get('time_end') ? $this->input->get('time_end',true) : '';
        $tag  = $this->input->get('tag') ? $this->input->get('tag',true) : '';

        $perpage  = 20;
        $offset = ($page - 1) * $perpage;

        $this->data['typeid'] = $typeid;
        $this->data['writer'] = $writer;
        $this->data['title']  = $title;
        $this->data['is_del'] = $is_del;
        $this->data['time_start']  = $time_start;
        $this->data['time_end']  = $time_end;
        $this->data['tag']  = $tag;

        $time_start && $time_start = strtotime($time_start.' 00:00:00');
        $time_end && $time_end = strtotime($time_end.' 23:59:59');

        $str = http_build_query(array('typeid'=>$typeid,'writer'=>$writer,'title'=>$title,'typeid'=>$typeid,'is_del'=>$is_del,'time_start'=>$time_start,'time_end'=>$time_end,'tag'=>$tag));


        $count = $this->article_model->get_count($typeid,$writer,$title,$typeid,$is_del,$time_start,$time_end,$tag);

        if(!empty($count)){
            $this->data['count']  = $count;
            $result = $this->article_model->get_page($typeid,$writer,$title,$typeid,$is_del,$time_start,$time_end,$tag,$perpage,$offset);
            $url = '/index.php?d=admin&c=article_list&'.$str;
            $this->data['multi']      = multi($count,$perpage,$page,$url);
            $this->data['date']       = $result;

            foreach($this->type_model->getList() as $v){
                $type_arr[$v['catid']] = $v;
            }
            $this->data['type_arr'] = $type_arr;
        }

        foreach($this->admin_model->get_all_admin() as $v){
            $admin_arr[$v['userid']] = $v['nickname'];
        }
        $this->data['admin_arr'] = $admin_arr;


        $this->data['op'] = 'list';
        $this->load->view('admin/article_list', $this->data);
    }

    /**
     * auto:xg
     * 将文章放入回收站
     */
    public function rec(){
        $id =  $this->input->get('id') ? $this->input->get('id',true) : '';
        $flag =  $this->input->get('flag') ? $this->input->get('flag',true) : 0;
        $is_pl =  $this->input->get('is_pl') ? $this->input->get('is_pl',true) : 0;//是否为批量操作  1为批量 ，0为单个操作
        if($is_pl == 0){
            $return = $this->article_model->set_is_del($id,$flag);
            if ($return == 1) {
                cpmessage("操作成功", "/index.php?d=admin&c=article_list");
            } else {
                cpmessage("操作失败", "/index.php?d=admin&c=article_list");
            }
        }else{

            $this->article_model->set_is_del($id,$flag);
        }
    }

    /**
     * auto:xg
     * 批量将文章放入回收站
     */
    public function rec_all(){
        $ids =  $this->input->get('ids') ? $this->input->get('ids',true) : '';
        $fl =  $this->input->get('fl') ? $this->input->get('fl',true) : 0;
        $ids = trim($ids,',');
        $ids_arr = explode(',',$ids);

        foreach($ids_arr as $v){
            if(!empty($v)){
                $this->rec($v,$fl,1);
            }
        }
        echo 3;exit;
    }

    /**
     * auto:xg
     * 批量添加标签(包括flag字段和mark字段，由$ziduan决定更新哪个字段),do决定是添加还是删除；
     */
    public function setflag(){
        $ids =  $this->input->get('ids') ? $this->input->get('ids',true) : '';
        $fl =  $this->input->get('fl') ? $this->input->get('fl',true) : 0;
        $ziduan =  $this->input->get('ziduan') ? $this->input->get('ziduan',true) : 0;
        $caozuo =  $this->input->get('caozuo') ? $this->input->get('caozuo',true) : '';

        $ids = trim($ids,',');
        $ids_arr = explode(',',$ids);
        foreach($ids_arr as $v){
            if(!empty($v)){
                if($caozuo == 1){
                    $this->article_model->add_flag_mark($v,$fl,$ziduan);
                }elseif($caozuo == 2){
                    $this->article_model->del_flag_mark($v,$fl,$ziduan);
                }
            }
        }
        echo 3;exit;
    }

    /**
     * auto:xg
     * 批量删除标签()
     */


}